package utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesReader {

    private static Logger logger = LoggerFactory.getLogger(PropertiesReader.class);

    private static Properties props = new Properties();
    private static String fileName = "conf.properties";

    static {
        String path = "/" + fileName;
        try {
            InputStream in = PropertiesReader.class.getResourceAsStream(path);
            props.load(in);
        } catch (IOException e) {
            logger.error("load properties file【" + fileName + "】error...");
        }
    }

    /**
     * 获取字符串类型的值
     * @param key 键
     * @return
     */
    public static String get(String key) {
        String value = props.getProperty(key);
        return null == value ? null : value;
    }

    /**
     * 获取Integer类型的值
     * @param key 键
     * @return
     */
    public static Integer getInteger(String key) {
        String value = get(key);
        return null == value ? null : Integer.valueOf(value);
    }

    /**
     * 获取Boolean类型的值
     * @param key
     * @return
     */
    public static Boolean getBoolean(String key) {
        String value = get(key);
        return null == value ? null : Boolean.valueOf(value);
    }

    public static void main(String[] args) {
        System.out.println(PropertiesReader.get("error.data.tablename"));
        System.out.println(PropertiesReader.get("error.data.db.driver"));
        System.out.println(PropertiesReader.get("error.data.db.url"));
        System.out.println(PropertiesReader.get("error.data.db.username"));
        System.out.println(PropertiesReader.get("error.data.db.password"));
    }
}
